package com.woniu.platform.service.impl;

import com.woniu.customer.entity.Customer;
import com.woniu.customer.entity.vo.CustomerVO;
import com.woniu.customer.mapper.CustomerMapper;
import com.woniu.platform.entity.PlatformCustomerAuditRecord;
import com.woniu.platform.entity.vo.PlatformCustomerAuditRecordVO;
import com.woniu.platform.mapper.PlatformCustomerAuditRecordMapper;
import com.woniu.platform.service.PlatformCustomerService;
import com.woniu.util.utils.HttpResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PlatformCustomerServiceImpl implements PlatformCustomerService {
    @Autowired
    private PlatformCustomerAuditRecordMapper platformCustomerAuditRecordMapper;
    @Autowired
    private CustomerMapper customerMapper;

    /**
     * 通过属性查询待审核用户
     * @param customerVO
     * @return
     */
    @Override
    public HttpResult<?> selectCustomerShouldAudit(CustomerVO customerVO) {
        try {
            customerVO.setStatus(0);
            List<Customer> customers = customerMapper.selectCustomer(customerVO);
            return new HttpResult<>().ok(customers);
        } catch (Exception e) {
            e.printStackTrace();
            return new HttpResult<>().fail();
        }
    }

    /**
     * 通过属性查找用户
     * @param customerVO
     * @return
     */
    @Override
    public HttpResult<?> selectCustomer(CustomerVO customerVO) {
        try {
            List<Customer> customers = customerMapper.selectCustomer(customerVO);
            return new HttpResult<>().ok(customers);
        } catch (Exception e) {
            e.printStackTrace();
            return new HttpResult<>().fail();
        }
    }

    /**
     * 审核用户
     * 生成审核记录
     * @param result
     * @param customerId
     * @return
     */
    @Override
    public HttpResult<?> customerAudit(Integer result, String customerId) {
        try {
            Customer customer = customerMapper.selectById(customerId);
            customer.setStatus(result);
            PlatformCustomerAuditRecord platformCustomerAuditRecord=new PlatformCustomerAuditRecord();
            platformCustomerAuditRecord.setCustomerId(customerId);
            platformCustomerAuditRecord.setPlatformUserId(1);//审核人员id（未完成）
            platformCustomerAuditRecord.setAuditResults(result);
            platformCustomerAuditRecordMapper.insert(platformCustomerAuditRecord);
            return new HttpResult<>().ok();
        } catch (Exception e) {
            e.printStackTrace();
            return new HttpResult<>().fail();
        }
    }

    /**
     * 通过属性查询客户审核记录
     * @param platformCustomerAuditRecordVO
     * @return
     */
    @Override
    public HttpResult<?> selectCustomerAuditRecord(PlatformCustomerAuditRecordVO platformCustomerAuditRecordVO) {
        try {
            List<PlatformCustomerAuditRecord> platformCustomerAuditRecords = platformCustomerAuditRecordMapper.selectCustomerAuditRecord(platformCustomerAuditRecordVO);
            return new HttpResult<>().ok(platformCustomerAuditRecords);
        } catch (Exception e) {
            e.printStackTrace();
            return new HttpResult<>().fail();
        }
    }
}
